<script setup>
import { artGetChannelService, artDelChannelService } from '@/api/article.js'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'

const channelList = ref([]) // 数据
const loading = ref(false) // 加载
const dialog = ref() // 弹窗

const getList = async () => {
  loading.value = true
  const res = await artGetChannelService()
  channelList.value = res.data.data
  loading.value = false
}
getList()

// 编辑
const onEditChannel = (row) => {
  dialog.value.open(row)
}
// 添加分类
const addChannel = () => {
  dialog.value.open({})
}
// 删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('您确定要删除该分类么？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  getList()
  ElMessage.success('删除成功')
}
// 重新渲染
const onSuccess = () => {
  getList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="addChannel" type="primary"> 添加分类 </el-button>
    </template>

    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>
